package com.sgb.kjwl.view.ui.fragment.pick;

import android.Manifest;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.apeng.permissions.EsayPermissions;
import com.apeng.permissions.OnPermission;
import com.chuanglan.shanyan_sdk.OneKeyLoginManager;
import com.chuanglan.shanyan_sdk.listener.OneKeyLoginListener;
import com.chuanglan.shanyan_sdk.listener.OpenLoginAuthListener;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.GsonManager;
import com.sgb.kjwl.databinding.FragmentPickRequireList3Binding;
import com.sgb.kjwl.di.pick.DaggerPickFragComponent;
import com.sgb.kjwl.di.pick.PickFragModule;
import com.sgb.kjwl.model.entity.LoginEntity;
import com.sgb.kjwl.model.entity.PickDeviceEntity2;
import com.sgb.kjwl.model.entity.PickLabourEntity;
import com.sgb.kjwl.model.entity.PickPackersEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.ProjectBuyEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.OneKeyLoginEntity;
import com.sgb.kjwl.model.entity.user.PickMaterialClassEntity;
import com.sgb.kjwl.utils.ConfigUtils;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.view.ui.activity.pick.PickCompanyDetailActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickProjectActivity;
import com.sgb.kjwl.view.ui.activity.pick.QuoteImmediatelyActivity;
import com.sgb.kjwl.view.ui.activity.self.authentication.AuthActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickDeviceAdapter2;
import com.sgb.kjwl.view.ui.adapter.pick.PickLabourAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickMaterialClassAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickPackersAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.ProjectBuyAdapter;
import com.swgk.core.mmkvutil.CacheMarker;
import com.sgb.kjwl.view.widget.DictWindow5;
import com.sgb.kjwl.view.widget.PickAreaPopWindow;
import com.sgb.kjwl.viewmodel.pick.PickRequireListFragModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Description:
 * Author zhengkewen
 */
public class PickRequireListFragment3 extends AppFragment {
    public static String KEY_FID = "fId";
    public static String KEY_FNAME = "fName";
    @Inject
    PickRequireListFragModel3 viewModel;
    private FragmentPickRequireList3Binding binding;
    private String fId;
    private String fName;
    private DictWindow5 engineeringWindow5;
    private String engineeringId;
    private List<DictEntity3> enginneringDictEntityList;
    private DictWindow5 scaleWindow5;
    private List<DictEntity3> scaleDictEntityList;
    private String scaleId;
    private PickAreaPopWindow areaMultiWindow5;
    private List<PickSelectEntity> areaDictEntityList = new ArrayList<>();
    private List<PickSelectEntity> cityDatas = new ArrayList<>();
    private String areaId;
    private DictWindow5 typeWindow5;
    private String typeId;
    private List<DictEntity3> typeDictEntityList;

    private ProjectBuyAdapter adapter1;
    private PickMaterialClassAdapter adapter2;
    private PickDeviceAdapter2 adapter3;
    private PickLabourAdapter adapter4;
    private PickPackersAdapter adapter5;
    private boolean isChange1;
    private boolean isChange2;
    private boolean isChange3;
    private boolean isChange4;
    //省
    private String areacode;
    private String cityNo;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPickFragComponent.builder()
                .appComponent(appComponent)
                .pickFragModule(new PickFragModule(this))
                .build()
                .inject(this);
    }

    public static PickRequireListFragment3 newInstance(String fId, String fName) {
        PickRequireListFragment3 fragment = new PickRequireListFragment3();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_FID, fId);
        bundle.putString(KEY_FNAME, fName);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_require_list3, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        fId = getArguments().getString(KEY_FID);
        fName = getArguments().getString(KEY_FNAME);
        initView();
        init();
    }

    private void initView() {
        binding.engineeringView.setOnClickListener(this);
        binding.scaleView.setOnClickListener(this);
        binding.areaView.setOnClickListener(this);
        binding.typeView.setOnClickListener(this);
        binding.imgBackTop.setOnClickListener(this);

        if (adapter1 == null) {
            adapter1 = new ProjectBuyAdapter(getActivity(), null);
        }
        if (adapter2 == null) {
            adapter2 = new PickMaterialClassAdapter(getActivity(), null);
        }
        adapter2.IOffer(new PickMaterialClassAdapter.IOffer() {
            @Override
            public void checkLogin(PickMaterialClassEntity entity, int id) {
                SharedPreferenceUtil.init(getActivity());
                String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, "");
                if (!"".equals(token)) {
                    if ("1".equals(entity.getStatusCode())) {
                        AuthActivity.start(getActivity());
                    } else if ("3".equals(entity.getStatusCode())) {
                        QuoteImmediatelyActivity.startMateria(getActivity(), entity, id);
                    }
                } else {
                    jumpLogin();
                }
            }
        });
        adapter2.setonEditClick(new PickMaterialClassAdapter.onEditClickListener() {
            @Override
            public void onProjectInfoClick(PickMaterialClassEntity entity, int position) {
                PickProjectActivity.start(getActivity(), entity.getProjNo());
            }

            @Override
            public void onBuyCompanyClick(PickMaterialClassEntity entity, int position) {
                PickCompanyDetailActivity.start(getActivity(), entity.getCompNo());
            }
        });
        if (adapter3 == null) {
            adapter3 = new PickDeviceAdapter2(getActivity(), null);
        }
        adapter3.IOffer(new PickDeviceAdapter2.IOffer() {
            @Override
            public void checkLogin(PickDeviceEntity2 entity, int id) {
                SharedPreferenceUtil.init(getActivity());
                String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, "");
                if (!"".equals(token)) {
                    if ("1".equals(entity.getStatusCode())) {
                        AuthActivity.start(getActivity());
                    } else if ("3".equals(entity.getStatusCode())) {
                        QuoteImmediatelyActivity.startDevice(getActivity(), entity, id);
                    }
                } else {
                    jumpLogin();
                }
            }
        });
        if (adapter4 == null) {
            adapter4 = new PickLabourAdapter(getActivity(), null);
        }
        adapter4.IOffer(new PickLabourAdapter.IOffer() {
            @Override
            public void checkLogin(PickLabourEntity entity, int id) {
                SharedPreferenceUtil.init(getActivity());
                String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, "");
                if (!"".equals(token)) {
                    if ("1".equals(entity.getStatusCode())) {
                        AuthActivity.start(getActivity());
                    } else if ("3".equals(entity.getStatusCode())) {
                        QuoteImmediatelyActivity.startLabour(getActivity(), entity, id);
                    }
                } else {
                    jumpLogin();
                }
            }
        });
        if (adapter5 == null) {
            adapter5 = new PickPackersAdapter(getActivity(), null);
        }
        adapter5.IOffer(new PickPackersAdapter.IOffer() {
            @Override
            public void checkLogin(PickPackersEntity entity, int id) {
                SharedPreferenceUtil.init(getActivity());
                String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, "");
                if (!"".equals(token)) {
                    if ("1".equals(entity.getStatusCode())) {
                        AuthActivity.start(getActivity());
                    } else if ("3".equals(entity.getStatusCode())) {
                        QuoteImmediatelyActivity.startActivity(getActivity(), entity, id);
                    }
                } else {
                    jumpLogin();
                }
            }
        });

        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if (fId.equals("1")) {
                    viewModel.getList1(false, areacode);
                } else if (fId.equals("2")) {
                    viewModel.getList2(false, areacode);
                } else if (fId.equals("3")) {
                    viewModel.getList3(false, areacode);
                } else if (fId.equals("4")) {
                    viewModel.getList4(false, areacode);
                } else if (fId.equals("5")) {
                    viewModel.getList5(false, areacode);
                }
            }

            @Override
            public void onLoadMore() {
                if (fId.equals("1")) {
                    viewModel.getList1(true, areacode);
                } else if (fId.equals("2")) {
                    viewModel.getList2(true, areacode);
                } else if (fId.equals("3")) {
                    viewModel.getList3(true, areacode);
                } else if (fId.equals("4")) {
                    viewModel.getList4(true, areacode);
                } else if (fId.equals("5")) {
                    viewModel.getList5(true, areacode);
                }
            }
        });

        if (fId.equals("1")) {
            binding.typeView.setVisibility(View.GONE);
            binding.recyclerView.setAdapter(adapter1);
        } else if (fId.equals("2")) {
            binding.engineeringView.setVisibility(View.GONE);
            binding.scaleView.setVisibility(View.GONE);
            binding.recyclerView.setAdapter(adapter2);
        } else if (fId.equals("3")) {
            binding.typeView.setVisibility(View.GONE);
            binding.engineeringView.setVisibility(View.GONE);
            binding.scaleView.setVisibility(View.GONE);
            binding.recyclerView.setAdapter(adapter3);
        } else if (fId.equals("4")) {
            binding.typeView.setVisibility(View.GONE);
            binding.engineeringView.setVisibility(View.GONE);
            binding.scaleView.setVisibility(View.GONE);
            binding.recyclerView.setAdapter(adapter4);
        } else if (fId.equals("5")) {
            binding.typeView.setVisibility(View.GONE);
            binding.scaleView.setVisibility(View.GONE);
            binding.recyclerView.setAdapter(adapter5);
        }
        initFilterView();

        binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                switch (newState){
                    /*正在拖拽*/
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        /*惯性滑动中*/
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        int position = manager.findFirstVisibleItemPosition();
                        if (position > 8) {
                            binding.imgBackTop.setVisibility(View.VISIBLE);
                        } else {
                            binding.imgBackTop.setVisibility(View.GONE);
                        }
                        break;
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

            }
        });
    }


    private void initFilterView() {
        engineeringWindow5 = new DictWindow5(getActivity());
        engineeringWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                engineeringId = dictEntity.getId();
                binding.engineeringView.setText(dictEntity.getName());
                if (fName.equals("工程招采")) {
                    viewModel.listEntity1.get().setType(dictEntity.getName());
                    viewModel.getList1(false, areacode);
                } else if (fName.equals("工程施工")) {
                    viewModel.listEntity5.get().setType(dictEntity.getName());
                    viewModel.getList5(false, areacode);
                }
                isChange1 = true;
                setTextViewBg(binding.engineeringView, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            }
        });
        engineeringWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                if (!isChange1) {
                    binding.engineeringView.setTextColor(getResources().getColor(R.color.color_333333));
                    setDrwImage(binding.engineeringView, 1);
                }else{
                    setDrwImage(binding.engineeringView, 2);
                }

            }
        });
        engineeringWindow5.setPickQuestion();
        typeWindow5 = new DictWindow5(getActivity());
        typeWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                typeId = dictEntity.getId();
                binding.typeView.setText(dictEntity.getName());
                if (fName.equals("材料")) {
                    viewModel.listEntity2.get().setType(dictEntity.getName());
                    viewModel.getList2(false, areacode);
                }
                isChange2 = true;
                setTextViewBg(binding.typeView, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            }
        });
        typeWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                if (!isChange2) {
                    binding.typeView.setTextColor(getResources().getColor(R.color.color_333333));
                    setDrwImage(binding.typeView, 1);
                }else{
                    setDrwImage(binding.typeView, 2);
                }
            }
        });
        typeWindow5.setPickQuestion();
        scaleWindow5 = new DictWindow5(getActivity());
        scaleWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                scaleId = dictEntity.getId();
                List<String> ids = new ArrayList<>();
                ids.add(dictEntity.getId());
                if (dictEntity.getName().equals("全部")) {
                    ids = null;
                }
                binding.scaleView.setText(dictEntity.getName());
                if (fName.equals("工程招采")) {
                    viewModel.listEntity1.get().setScaleNo(ids);
                    viewModel.getList1(false, areacode);
                }
                isChange3 = true;
                setTextViewBg(binding.scaleView, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            }
        });
        scaleWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                if (!isChange3) {
                    binding.scaleView.setTextColor(getResources().getColor(R.color.color_333333));
                    setDrwImage(binding.scaleView, 1);
                }else{
                    setDrwImage(binding.scaleView, 2);
                }
            }
        });
        scaleWindow5.setPickQuestion();
        areaMultiWindow5 = new PickAreaPopWindow(getActivity());
        areaMultiWindow5.isVisibleBottomLayout(true);
        areaMultiWindow5.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                List<String> ids = new ArrayList<>();
                String name = (String) key2;
                boolean isCity = (boolean) key3;
                if (isCity) {
                    areacode = (String) key1;
                    cityNo = "";
                    ids = null;
                } else {
                    cityNo = (String) key1;
                    ids.add(cityNo);
                }
                binding.areaView.setText(name);
                if (fId.equals("1")) {
                    viewModel.listEntity1.get().setCityNo(ids);
                    viewModel.getList1(false, areacode);
                } else if (fId.equals("2")) {
                    viewModel.listEntity2.get().setCityNo(ids);
                    viewModel.getList2(false, areacode);
                } else if (fId.equals("3")) {
                    viewModel.listEntity3.get().setCityNo(ids);
                    viewModel.getList3(false, areacode);
                } else if (fId.equals("4")) {
                    viewModel.listEntity4.get().setCityNo(ids);
                    viewModel.getList4(false, areacode);
                } else if (fId.equals("5")) {
                    viewModel.listEntity5.get().setCityNo(ids);
                    viewModel.getList5(false, areacode);
                }
                isChange4 = true;
                setTextViewBg(binding.areaView, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            }
        });
        areaMultiWindow5.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                if (!isChange4) {
                    binding.areaView.setTextColor(getResources().getColor(R.color.color_tab_one));
                    setDrwImage(binding.areaView, 1);
                }else{
                    setDrwImage(binding.areaView, 2);
                }
            }
        });
        areaMultiWindow5.setiButtonClick(new PickAreaPopWindow.IButtonClick() {
            @Override
            public void next(String no) {
                viewModel.getCityData(no);
            }
        });
    }

    private void init() {
        if (fId.equals("1")) {
            viewModel.getList1(false, areacode);
        } else if (fId.equals("2")) {
            viewModel.getList2(false, areacode);
        } else if (fId.equals("3")) {
            viewModel.getList3(false, areacode);
        } else if (fId.equals("4")) {
            viewModel.getList4(false, areacode);
        } else if (fId.equals("5")) {
            viewModel.getList5(false, areacode);
        }
    }

    public void showTop(boolean isLoadMore) {
        if (isLoadMore) {
            binding.imgBackTop.setVisibility(View.VISIBLE);
        } else {
            binding.imgBackTop.setVisibility(View.GONE);
        }
    }

    //工程招采列表
    public void showList1(List<ProjectBuyEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter1.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            adapter1.setDatas(list);
        }
    }

    //材料列表
    public void showList2(List<PickMaterialClassEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter2.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            adapter2.setDatas(list);
        }
    }

    //租赁列表
    public void showList3(List<PickDeviceEntity2> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter3.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            adapter3.setDatas(list);
        }
    }

    //劳务列表
    public void showList4(List<PickLabourEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter4.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            adapter4.setDatas(list);
        }
    }

    //工程施工列表
    public void showList5(List<PickPackersEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter5.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            adapter5.setDatas(list);
        }
    }

    public void showEngineeringPopWindow(List<DictEntity3> data) {
        this.enginneringDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        engineeringWindow5.setData(data);
    }

    public void showTypePopWindow(List<DictEntity3> data) {
        this.typeDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        typeWindow5.setData(data);
    }

    public void showScalePopWindow(List<DictEntity3> data) {
        this.scaleDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        scaleWindow5.setData(data);
    }

    public void showAreaPopWindow() {
        if (areaDictEntityList == null || areaDictEntityList.size() == 0) {
            return;
        }
        areaMultiWindow5.setData(areaDictEntityList);
    }

    public void setCityDatas(List<PickSelectEntity> datas) {
        cityDatas.clear();
        cityDatas.addAll(datas);
        areaMultiWindow5.setCityDatas(cityDatas);
    }

    public void setProvinceDatas(List<PickSelectEntity> datas) {
        areaDictEntityList.clear();
        areaDictEntityList.addAll(datas);
        showAreaPopWindow();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.engineering_view:
                if (!isChange1) {  //未选中结果
                    if (!engineeringWindow5.isShowing()) {
                        binding.engineeringView.setTextColor(getResources().getColor(R.color.color_main));
                        setDrwImage(binding.engineeringView, 0);
                    }
                } else {
                    if (!engineeringWindow5.isShowing()) {
                        setDrwImage(binding.engineeringView, 0);
                    }
                }
                engineeringWindow5.setCheckDefaultById(engineeringId);
                engineeringWindow5.showPopwindow(binding.rootView);
                if (enginneringDictEntityList == null) {
                    viewModel.getFilterEngineeringType();
                }
                break;
            case R.id.type_view:
                if (!isChange2) {
                    if (!typeWindow5.isShowing()) {
                        binding.typeView.setTextColor(getResources().getColor(R.color.color_main));
                        setDrwImage(binding.typeView, 0);
                    }
                } else {
                    if (!typeWindow5.isShowing()) {
                        setDrwImage(binding.typeView, 0);
                    }
                }
                typeWindow5.setCheckDefaultById(typeId);
                typeWindow5.showPopwindow(binding.rootView);
                if (typeDictEntityList == null) {
                    viewModel.getFilterMaterialType();
                }
                break;
            case R.id.area_view:
                if (!isChange4) {
                    if (!areaMultiWindow5.isShowing()) {
                        binding.areaView.setTextColor(getResources().getColor(R.color.color_main));
                        setDrwImage(binding.areaView, 0);
                    }
                } else {
                    if (!areaMultiWindow5.isShowing()) {
                        setDrwImage(binding.areaView, 0);
                    }
                }
                areaMultiWindow5.showPopwindow(binding.rootView);
                if (areaDictEntityList.size() == 0) {
                    viewModel.getProvinceData();
                } else {
                    showAreaPopWindow();
                }
                break;
            case R.id.scale_view:
                if (!isChange4) {
                    if (!scaleWindow5.isShowing()) {
                        binding.scaleView.setTextColor(getResources().getColor(R.color.color_main));
                        setDrwImage(binding.scaleView, 0);
                    }
                } else {
                    if (!scaleWindow5.isShowing()) {
                        setDrwImage(binding.scaleView, 0);
                    }
                }
                scaleWindow5.setCheckDefaultById(scaleId);
                scaleWindow5.showPopwindow(binding.rootView);
                if (scaleDictEntityList == null) {
                    viewModel.getFilterScale();
                }
                break;
            case R.id.img_back_top:
                binding.imgBackTop.setVisibility(View.GONE);
                binding.recyclerView.getLayoutManager().scrollToPosition(0);
                break;
        }
    }

    public void jumpLogin() {
        boolean shanyan = MkvUtils.get().getBoolean(MkvUtils.CommomData, CacheMarker.ShanYan, false);
        if (shanyan) {
            //校验权限
            //申请动态权限
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //6.0以上运行时权限处理
                EsayPermissions.with(getActivity()).constantRequest().permission(PERMISSIONS).request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        openLoginPage();
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        openLoginPage();
                    }
                });

            } else {
                //有权限
                openLoginPage();
            }

        } else {
            VerificationCodeLoginActivity.start(getActivity());
        }
    }

    //闪验
    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.READ_PHONE_STATE
    };

    /**
     * 拉起授权页
     */
    private void openLoginPage() {
        //设置授权页样式
        OneKeyLoginManager.getInstance().setAuthThemeConfig(ConfigUtils.getCJSConfig(getActivity()));
        //开始拉取授权页
        DialogHelper.showProgressDialog(getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);
        OneKeyLoginManager.getInstance().openLoginAuth(false, new OpenLoginAuthListener() {
            @Override
            public void getOpenLoginAuthStatus(int code, String result) {
                DialogHelper.dismissProgressDialog();
                if (code != 1000) {
                    VerificationCodeLoginActivity.start(getActivity());
                }
            }
        }, new OneKeyLoginListener() {
            @Override
            public void getOneKeyLoginStatus(int code, String result) {
                DialogHelper.dismissProgressDialog();
                if (code == 1000) {
                    OneKeyLoginEntity oneKeyLoginEntity = GsonManager.getInstance().getGson().fromJson(result, OneKeyLoginEntity.class);
                    oneKeyLoginEntity.setPlateform("Android");
                    viewModel.oneKeyLogin(oneKeyLoginEntity);
                }
            }
        });
    }

    /**
     * 一键登录成功
     *
     * @param loginEntity
     * @param phoneNumber
     */
    public void loginSucceed(LoginEntity loginEntity, String phoneNumber) {
        LoginEvent loginEvent = new LoginEvent();
        loginEvent.setMessageData(phoneNumber);
        loginEvent.setMessageTag(EventConstant.EVENT_LOGIN);
        loginEvent.setObject(loginEntity);
        EventBus.getDefault().postSticky(loginEvent);
    }

    public void getUserMsgSucceed(UserMsgEntity userMsgEntity) {
        LoginEvent loginEvent = new LoginEvent();
        loginEvent.setMessageTag(EventConstant.EVENT_USER_MSG);
        loginEvent.setMessageData(userMsgEntity);
        EventBus.getDefault().postSticky(loginEvent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void setTextViewBg(TextView textView, int bg, int img, int textColor) {
        if(isAdded()){
            textView.setBackground(getResources().getDrawable(bg));
            Drawable drawable_n = getResources().getDrawable(img);
            drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());  //此为必须写的
            textView.setCompoundDrawables(null, null, drawable_n, null);
            textView.setTextColor(getResources().getColor(textColor));
        }
    }

    private void setDrwImage(TextView tv, int type) {
        Drawable img_off = null;
        Resources res = getResources();

        if (type == 0) {
            img_off = res.getDrawable(R.mipmap.icon_up_red);
        } else if (type == 1) {
            img_off = res.getDrawable(R.mipmap.icon_up_red);
        } else if (type == 2) {
            img_off = res.getDrawable(R.mipmap.icon_down_red);
        }

        img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
        tv.setCompoundDrawables(null, null, img_off, null); //设置右图标
    }
}
